home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre2.z / postgre2 / ref / postquel / defineaggregat < prev    next >
Encoding:
Text File  |  1992-08-27  |  2.1 KB  |  72 lines

  1. .\" XXX standard disclaimer belongs here....
  2. .\" $Header: /private/postgres/ref/postquel/RCS/defineaggregate,v 1.11 1992/07/14 05:54:17 ptong Exp $
  3. .SP "DEFINE AGGREGATE" COMMANDS 6/14/90
  4. .XA 2 "Define Aggregate"
  5. .uh NAME
  6. .lp
  7. define aggregate \*- define a new aggregate
  8. .uh SYNOPSIS
  9. .lp
  10. .(l
  11. \fBdefine aggregate\fR agg-name [\fBas\fR]
  12.     \fB(\fR\fBsfunc1\fR \fB=\fR state-transition-function1,
  13.      \fBsfunc2\fR \fB=\fR state-transition-func2,
  14.      \fBfinalfunc\fR \fB=\fR final-function,
  15.      \fBinitcond1\fR \fB=\fR initial-condition1,
  16.      \fBinitcond2\fR \fB=\fR initial-condition2 \fB)\fR
  17. .)l
  18. .uh DESCRIPTION
  19. .lp
  20. An aggregate requires three functions, two
  21. .i "state transition"
  22. functions, X1 and X2:
  23. .(l
  24. X1( internal-state1, next-data_item ) ---> next-internal-state1
  25. X2( internal-state2 ) ---> next-internal-state2
  26. .)l
  27. .lp
  28. and a
  29. .b "final calculation"
  30. function, F:
  31. .(l
  32. F(internal-state1, internal-state2) ---> aggregate-value
  33. .)l
  34. .lp
  35. These functions are required to have the following three properties:
  36. .np
  37. The return type of each state-transition-function and the arguments
  38. of the final-calculation-function must be the same type (\fIt\fP).
  39. .np
  40. The return type of the final-calculation-function must be a \*(PP base
  41. type.
  42. .np
  43. The first argument to state-transition-function1 must be of type \fIt\fP,
  44. while the second argument must match the data type of the object
  45. being aggregated.
  46. .lp
  47. Aggregates also require two initial conditions, one for each transition 
  48. function.
  49. .uh EXAMPLE
  50. .lp
  51. The
  52. .i average
  53. aggregate would consist of two state
  54. transition functions, a summer and an incrementer.  These would
  55. hold the internal state of the aggregate through a 
  56. running sum and and the number of values seen so far.
  57. It might accept a new employee salary,
  58. increment the count, and add the new salary to
  59. produce the next state.  The state transition functions must be
  60. passed correct initialization values.  The final calculation 
  61. then divides the sum by the count to produce the final
  62. answer.
  63. .lp
  64. .ft C
  65. /* Define an aggregate for int4average */
  66. .ft
  67. .lp
  68. .ft C
  69. define aggregate avg (sfunc1 = int4add, sfunc2 = int4inc
  70.      finalfunc = int4div, initcond1 = "0", initcond2 = "0")
  71. .fn
  72.